package com.joe.gulishop.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.joe.gulishop.product.dao.AttrAttrgroupRelationDao;
import com.joe.gulishop.product.dao.AttrDao;
import com.joe.gulishop.product.entity.AttrEntity;
import com.joe.gulishop.product.service.AttrAttrgroupRelationService;
import com.joe.gulishop.product.service.AttrService;
import com.joe.gulishop.product.service.CategoryService;
import com.joe.gulishop.product.vo.AttrGroupRelationVo;
import com.joe.gulishop.product.vo.AttrGroupWithAttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.joe.gulishop.product.entity.AttrGroupEntity;
import com.joe.gulishop.product.service.AttrGroupService;
import com.joe.common.utils.PageUtils;
import com.joe.common.utils.R;



/**
 * 属性分组
 *
 * @author joe
 * @email joe@gmail.com
 * @date 2021-12-03 10:45:56
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService relationService;
    /**
     * 列表,使用分页查询和模糊查询产生的map进行多条件的筛选
     */
    @RequestMapping("/list/{cateLogId}")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("cateLogId")Long cateLogId){

     //   PageUtils page = attrGroupService.queryPage(params);

        PageUtils pageUtils = attrGroupService.queryPage(params, cateLogId);


        return R.ok().put("page", pageUtils);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
		Long catelogId = attrGroup.getCatelogId();
		Long[] cateLogIdList=categoryService.findCatelogPath(catelogId);
        attrGroup.setCatelogPath(cateLogIdList);
		return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

/*
* 设置关联关系
*
* */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId")Long attrgroupId){




        List<AttrEntity> entities = attrService.getRelationAttr(attrgroupId);

        return R.ok().put("data",entities);

    }
//    获取没有关联的
    //http://localhost:88/api/product/attrgroup/1/noattr/relation?t=1642928057146&page=1&limit=10&key=
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R noattrRelation(@PathVariable("attrgroupId")Long attrgroupId,
                            @RequestParam Map<String, Object> params
    ){
        PageUtils pageUtils=attrService.getNoRelationAttr(attrgroupId,params);
        System.out.println(pageUtils.getList().size());



        return R.ok().put("page",pageUtils);
    }
    ///product/attrgroup/attr/relation/delete
    @PostMapping("/attr/relation/delete")
    public R attrRelationDelete(@RequestBody AttrGroupRelationVo [] relationVos){

        attrGroupService.removeRelation(relationVos);

        return  R.ok();
    }

//   新增分组和属性关联
//    http://localhost:88/api/product/attrgroup/attr/relation
    @PostMapping("/attr/relation")
    public  R attrRelation(@RequestBody List<AttrGroupRelationVo> vos){
        relationService.saveBatch(vos);

        return R.ok();
    }

    @GetMapping("/{catelogId}/withattr")
    public R catelogIdWithattr(@PathVariable("catelogId")Long catelogId){
       List<AttrGroupWithAttrVo> attrs= attrGroupService.getCatelogIdWithattr(catelogId);

        return R.ok().put("data",attrs);
    }
}
